package com.design_mode;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

/**
 * Created by dustaple on 8/2/14.
 */
public class ObserverPattern {

    public static void main(String[] args) {
        Beauty sexyGirl = new Beauty();
        AbstractObserver observerA = new ObserverA();
        AbstractObserver observerB = new ObserverB();
        AbstractObserver observerC = new ObserverC();

        sexyGirl.addObserver(observerA);
        sexyGirl.addObserver(observerB);
        sexyGirl.addObserver(observerC);

        sexyGirl.singing();
        sexyGirl.dancing();
        sexyGirl.undressing();
        sexyGirl.shower();
    }
}

interface AbstractObserver {
    public abstract void update(String msg);
}

abstract class AbstractSubject {
    Vector<AbstractObserver> observers = new Vector<AbstractObserver>();
    String content;

    public final void addObserver(AbstractObserver observer) {
        observers.add(observer);
    }

    public final void removeObserver(AbstractObserver observer) {
        observers.remove(observer);
    }

    public final void setContent(String content) {
        this.content = content;
    }

    public final void notifyAlls() {
        System.out.println(content);
        Iterator<AbstractObserver> iterator = observers.iterator();
        while (iterator.hasNext()) {
            iterator.next().update(content);
        }
    }
}

class Beauty extends AbstractSubject {
    public void singing() {
        super.setContent("1.正在唱歌，歌声很优美！");
        notifyAlls();
    }

    public void dancing() {
        super.setContent("2.正在跳舞，身姿很妖娆！");
        notifyAlls();
    }

    public void undressing() {
        super.setContent("3.正在脱衣服，身材很撩人！");
        notifyAlls();
    }

    public void shower() {
        super.setContent("4.正在淋浴，动作很诱惑！");
        notifyAlls();
    }
}

class ObserverA implements AbstractObserver {

    @Override
    public void update(String msg) {
        char i = msg.charAt(0);
        switch (i) {
            case '1':
                System.out.println(getClass().getName() + "说：歌唱的好棒！");
                break;
            case '2':
                System.out.println(getClass().getName() + "说：舞跳得的好棒！");
                break;
            case '3':
                System.out.println(getClass().getName() + "说：都流口水了！");
                break;
            case '4':
                System.out.println(getClass().getName() + "说：受不了！！！");
                break;
            default:
                System.out.println(getClass().getName() + "说：真没意思!");
                break;
        }
    }
}

class ObserverB implements AbstractObserver {

    @Override
    public void update(String msg) {
        char ch = msg.charAt(0);
        int i = Integer.valueOf(String.valueOf(ch));
        switch (i) {
            case 1:
                System.out.println(getClass().getName() + "说：歌很好听！");
                break;
            case 2:
                System.out.println(getClass().getName() + "说：舞好劲爆啊！");
                break;
            case 3:
                System.out.println(getClass().getName() + "说：好大的尺度啊！");
                break;
            case 4:
                System.out.println(getClass().getName() + "说：要...！！！");
                break;
            default:
                System.out.println(getClass().getName() + "说：好没劲!");
                break;
        }
    }
}

class ObserverC implements AbstractObserver {

    @Override
    public void update(String msg) {
        char ch = msg.charAt(0);
        int i = Integer.valueOf(String.valueOf(ch));
        switch (i) {
            case 1:
                System.out.println(getClass().getName() + "说：天籁啊，真是！");
                break;
            case 2:
                System.out.println(getClass().getName() + "说：好风骚啊！");
                break;
            case 3:
                System.out.println(getClass().getName() + "说：要不要这么牛啊！");
                break;
            case 4:
                System.out.println(getClass().getName() + "说：去，要掉节操！");
                break;
            default:
                System.out.println(getClass().getName() + "说：好无聊!");
                break;
        }
    }
}